package com.lianzhihui.romeex.widget.keybord;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.user.WalletInfo;
import com.lianzhihui.romeex.database.DataBaseManager;
import com.lianzhihui.romeex.inter.CustomAllCallback;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.UserUtils;

import butterknife.BindView;

/**
 * @ProjectName: NBN
 * @ClassName: CreateNidDialog
 * @Description: 安全键盘弹窗
 * @Author: echo
 * @CreateDate: 2019/11/7 14:42
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/7 14:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SafetyKeyBoardDialog extends BaseDialog implements SafetyFinishInputListener {
    @BindView(R.id.safetyKeyBoardView)
    SafetyKeyBoardView safetyKeyBoardView;
    private String currentAddress;
    private CustomAllCallback callback;
    private String currentSafetyCode = "";
    private boolean inputStatus = false;
    WalletInfo walletInfo;

    public SafetyKeyBoardDialog(@NonNull Activity activity, String currentAddress) {
        super(activity);
        this.currentAddress = currentAddress;
    }

    public SafetyKeyBoardDialog(@NonNull Activity activity) {
        super(activity);
    }

    public void setCallback(CustomAllCallback callback) {
        this.callback = callback;
    }

    public boolean isInputStatus() {
        return inputStatus;
    }


    @Override
    protected int provideViewId() {
        return R.layout.dialog_safety_keyboard;
    }

    @Override
    protected void initView() {
        fullWidth();
        bottom();
        safetyKeyBoardView.showPasswodPanel(true);
        safetyKeyBoardView.setCloseAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onFailure("");
            }
        });
        safetyKeyBoardView.setOnInputFinishListener(this);

        if (TextUtils.isEmpty(currentAddress)) {
            walletInfo = UserUtils.getCurrentUser();
        } else {
            walletInfo = UserUtils.getUser(currentAddress);
            currentAddress = walletInfo.getAddress();
        }

    }

    public void setTitle(String title) {
        safetyKeyBoardView.setTitle(title);
    }

    public void showToolBar(View.OnClickListener onClickListener) {
        safetyKeyBoardView.showToolbarMore(true);
        safetyKeyBoardView.setMoreAction(onClickListener);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (null != callback) {
            callback.dissmiss();
        }
    }

    @Override
    public void onFinishInput(String word) {
        if (walletInfo == null) {
            callback.onFailure("无用户");
            safetyKeyBoardView.showError(true);
            return;
        }
        if (walletInfo.equSafeCode(word)) {
            inputStatus=true;
            callback.onCompare(word);
        } else {
            inputStatus = false;
            callback.onFailure("");
            safetyKeyBoardView.showError(true);
        }
    }

    public void setHideToolBarMor() {
        safetyKeyBoardView.showToolbarMore(false);
    }

}
